//
//  clsGhost.cpp
//  NewMath
//
//  Created by Ngô Hoàng Nam on 12/27/14.
//
//

#include "GhostResult.h"
#include "../../../GamePlayConfig.h"
#include "../../../GameConfig.h"
#include "../../../Framework/SoundManager.h"

GhostResult::GhostResult()
{
	//GhostObject::GhostObject();
}

void GhostResult::initalize(EGhostColor color, EGhostFaceState ghostFaceState, EGhostHandState ghostHandState, std::string equation, Vec2 position, float scale, bool win)
{
	GhostObject::initalize(color, ghostFaceState, ghostHandState, EGhostEyeColor::kGhostEyeColor_Random, position, scale, false);

	float newWidthGhost = kGhostSpriteWidth*m_scale;
	float newHeightGhost = kGhostSpriteHeight*m_scale;
	m_isWin = win;

	Size size = Director::getInstance()->getVisibleSize();

	if (win)
	{
		m_clound = Sprite::create("could_win.png");

		m_clound->setPosition(Vec2(size.width / 2, size.height * 0.75f));

		Label* lblCongratulation = Label::create("Congratulation!", kFont_ADLIBN, 80);
		lblCongratulation->setTextColor(Color4B::RED);
		lblCongratulation->setPosition(Vec2(m_clound->getContentSize().width / 2, -65));

		Label* lblLevelComplete = Label::create("Level complete!", kFont_ADLIBN, 60);
		lblLevelComplete->setTextColor(Color4B::BLUE);
		lblLevelComplete->setPosition(Vec2(m_clound->getContentSize().width / 2, lblCongratulation->getPositionY() - lblCongratulation->getContentSize().height / 2 - 40));

		Label* label = Label::create(equation, kFont_ADLIBN, 120);
		label->setPosition(m_clound->getContentSize().width / 2, m_clound->getContentSize().height / 3);
		label->setColor(Color3B(8, 120, 27));
		m_clound->addChild(label);

		m_clound->setScale(m_scale);
		getParent()->addChild(m_clound);
		m_clound->addChild(lblCongratulation);
		m_clound->addChild(lblLevelComplete);

		m_sprfail = Sprite::create("icon_lose.png");
		m_sprfail->setScale(m_scale);
		m_sprfail->setPosition(m_clound->getPosition());
		getParent()->addChild(m_sprfail);

		m_sprfail->setVisible(false);
	}
	else
	{
		m_clound = Sprite::create("could_lose.png");

		m_clound->setPosition(Vec2(size.width / 2, size.height * 0.75f));
		Label* label = Label::create(equation, kFont_ADLIBN, 120);
		label->setPosition(m_clound->getContentSize().width / 2, m_clound->getContentSize().height / 3);
		label->setColor(Color3B(183, 0, 0));
		m_clound->addChild(label);

		m_clound->setScale(m_scale);
		getParent()->addChild(m_clound);
		m_sprfail = Sprite::create("icon_lose.png");
		m_sprfail->setScale(m_scale);
		m_sprfail->setPosition(m_clound->getPosition());
		getParent()->addChild(m_sprfail);
	}
}

void GhostResult::runDisplayAction()
{
	m_body->setScale(0.0f);
	m_leftHand->setScale(0.0f);
	m_rightHand->setScale(0.0f);

	m_clound->setScale(0.0f);
	m_sprfail->setScale(0.0f);

	if (m_sprfail->isVisible())
	{
		auto ghostFunc = CallFuncN::create(CC_CALLBACK_0(GhostResult::runGhostAction, this));
		auto cloundFunc = CallFuncN::create(CC_CALLBACK_0(GhostResult::runCloundAction, this));
		auto failFunc = CallFuncN::create(CC_CALLBACK_0(GhostResult::runFailAction, this));
		this->runAction(Sequence::create(cloundFunc, DelayTime::create(1.0f), ghostFunc, DelayTime::create(1.0f), failFunc, NULL));
	}
	else
	{
		auto ghostFunc = CallFuncN::create(CC_CALLBACK_0(GhostResult::runGhostAction, this));
		auto cloundFunc = CallFuncN::create(CC_CALLBACK_0(GhostResult::runCloundAction, this));
		//auto failFunc = CallFuncN::create(CC_CALLBACK_0(GhostResult::runGhostAction, this));
		this->runAction(Sequence::create(DelayTime::create(0.5f), cloundFunc, DelayTime::create(0.5f), ghostFunc, NULL));
	}

}

void GhostResult::runGhostAction()
{
	auto scaleBody = Sequence::create(ScaleTo::create(0.1f, 1.3f * m_scale), ScaleTo::create(0.1f, 0.7f*m_scale), ScaleTo::create(0.1f, 1.2f* m_scale), ScaleTo::create(0.1f, 0.8f* m_scale), ScaleTo::create(0.1f, 1.1f* m_scale), ScaleTo::create(0.1f, 0.9f* m_scale), ScaleTo::create(0.1f, 1.0f* m_scale), NULL);
	auto scaleLeftHand = Sequence::create(ScaleTo::create(0.1f, 1.3f * m_scale), ScaleTo::create(0.1f, 0.7f*m_scale), ScaleTo::create(0.1f, 1.2f* m_scale), ScaleTo::create(0.1f, 0.8f* m_scale), ScaleTo::create(0.1f, 1.1f* m_scale), ScaleTo::create(0.1f, 0.9f* m_scale), ScaleTo::create(0.1f, 1.0f* m_scale), NULL);
	auto scaleRightHand = Sequence::create(ScaleTo::create(0.1f, 1.3f * m_scale), ScaleTo::create(0.1f, 0.7f*m_scale), ScaleTo::create(0.1f, 1.2f* m_scale), ScaleTo::create(0.1f, 0.8f* m_scale), ScaleTo::create(0.1f, 1.1f* m_scale), ScaleTo::create(0.1f, 0.9f* m_scale), ScaleTo::create(0.1f, 1.0f* m_scale), NULL);

	m_body->runAction(scaleBody);
	m_leftHand->runAction(scaleLeftHand);
	m_rightHand->runAction(scaleRightHand);

	if (m_isWin)
    {
		this->m_leftHand->runAction(RepeatForever::create(Sequence::create(RotateBy::create(0.2f, 30), RotateBy::create(0.3f, -30), NULL)));
		this->m_rightHand->runAction(RepeatForever::create(Sequence::create(RotateBy::create(0.2f, -30), RotateBy::create(0.3f, 30), NULL)));
		this->runAction(RepeatForever::create(Sequence::create(RotateTo::create(0.6f, -15), RotateTo::create(0.6f, 15), NULL)));

		auto happy = CallFuncN::create(CC_CALLBACK_0(GhostResult::setFaceHappy, this));
		auto idle = CallFuncN::create(CC_CALLBACK_0(GhostResult::setFaceIdle, this));

		this->runAction(RepeatForever::create(Sequence::create(DelayTime::create(1.0f), idle, DelayTime::create(1.0f), happy, NULL)));
        
        SoundManager::getInstance()->playSound(ESound::kSound_Yeah);
	}
	else
	{
		this->m_leftHand->runAction(RepeatForever::create(Sequence::create(RotateBy::create(0.2f, 30), RotateBy::create(0.3f, -30), NULL)));
		this->m_rightHand->runAction(RepeatForever::create(Sequence::create(RotateBy::create(0.2f, -30), RotateBy::create(0.3f, 30), NULL)));
		this->runAction(RepeatForever::create(Sequence::create(RotateTo::create(0.6f, -15), RotateTo::create(0.6f, 15), NULL)));
        
        SoundManager::getInstance()->playSound(ESound::kSound_Aww);
	}
	/*auto rotateBody = RepeatForever::create(Sequence::create(SkewTo::create(0.3f, 0, 5), SkewTo::create(0.3f, 0, -5), NULL));
	auto rotateLeftHand = RepeatForever::create(Sequence::create(SkewTo::create(0.3f, 0, 5), SkewTo::create(0.3f, 0, -5), NULL));
	auto rotateRightHand = RepeatForever::create(Sequence::create(SkewTo::create(0.3f, 0, 5), SkewTo::create(0.3f, 0, -5), NULL));
	m_body->runAction(rotateBody);
	m_leftHand->runAction(rotateLeftHand);
	m_rightHand->runAction(rotateRightHand); */
}

void GhostResult::runCloundAction()
{
	auto action = Sequence::create(ScaleTo::create(0.1f, 1.3f * m_scale), ScaleTo::create(0.1f, 0.7f*m_scale), ScaleTo::create(0.1f, 1.2f* m_scale), ScaleTo::create(0.1f, 0.8f* m_scale), ScaleTo::create(0.1f, 1.1f* m_scale), ScaleTo::create(0.1f, 0.9f* m_scale), ScaleTo::create(0.1f, 1.0f* m_scale), NULL);
	m_clound->runAction(action);
}

void GhostResult::runFailAction()
{
	m_sprfail->cocos2d::Node::setScale(2.0f*m_scale);
	auto action = ScaleTo::create(1.0f, 1.0f*m_scale);
	m_sprfail->runAction(action);
}

void GhostResult::setFaceHappy()
{
	GhostObject::setFace(EGhostFaceState::kGhostFaceState_Full, m_status);
}

void GhostResult::setFaceIdle()
{
	GhostObject::setFace(EGhostFaceState::kGhostFaceState_Free, EGhostStatus::kGhostStatus_Happy);
}